package cn.tedu.sp04orderservice.service;

import cn.tedu.entity.Item;
import cn.tedu.entity.Order;
import cn.tedu.entity.User;
import cn.tedu.service.OrderService;
import cn.tedu.sp04orderservice.fegin.ItemClient;
import cn.tedu.sp04orderservice.fegin.UserClient;
import cn.tedu.web.util.JsonResult;
import com.netflix.discovery.converters.Auto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private ItemClient itemClient;

    @Autowired
    private UserClient userClient;
    @Override
    public Order getOrder(String orderId) {
        //TODO: 远程调用商品.获取商品列表
        JsonResult<List<Item>> items = itemClient.getItems(orderId);
        //TODO: 远程调用用户.获取用户数据
        //正式项目中获取已经登录的用户ID
        JsonResult<User> user = userClient.getUser(8);
        log.info("获取订单"+orderId);
        Order order = new Order();
        order.setId(orderId);
        order.setItems(items.getData());
        order.setUser(user.getData());
        return order;
    }

    @Override
    public void addOrder(Order order) {
        log.info("添加订单"+order);
        //TODO: 远程调用商品.减少库存
        itemClient.decreaseNumber(order.getItems());
        //TODO: 远程调用用户 增加积分
        userClient.addScore(order.getUser().getId(),1000);
    }
}